﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Web.Settle
{
    public partial class Settle_AgentFee : System.Web.UI.Page
    {
        public double numCunRu=0;
        public double numZhiQu=0;
        public double numShiCun=0;
        public double numMoenyFee = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["WBID"] != null)
                {
                    txtWBID.Value = SQLHelper.ExecuteScalar(" SELECT  TOP 1 strName FROM dbo.WB WHERE ID=" + Request.QueryString["WBID"].ToString()).ToString();
                }
                if (Request.QueryString["WBName"] != null)
                {
                    string WBName = Request.QueryString["WBName"].ToString();
                    string dtStart = Request.QueryString["dtStart"].ToString();
                    string dtEnd = Request.QueryString["dtEnd"].ToString();

                }
                else
                {
                    Qdtend.Value = DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");
                    Qdtstart.Value = DateTime.Now.AddMonths(-7).ToString("yyyy-MM-dd");
                }
               
            }
        }
        private void BindData(string WBName,string dtStart,string dtEnd)
        {
            
            //当前网点最少要存储满多少天可以给网点代理费
            object objDay = SQLHelper.ExecuteScalar(" SELECT  numDay FROM dbo.WB WHERE strName ='" + WBName + "'");


            //获取存粮信息
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSqlPara = new StringBuilder();//查询参数
            strSql.Append("  SELECT A.ID, C.strName AS WBName,B.AccountNumber,B.strName,'存粮代理' AS BusinessName,CONVERT(NVARCHAR(100),A.StorageDate,23 ) AS  StorageDate, DATEDIFF(DAY,A.StorageDate,GETDATE()) AS StorageDay,");
            strSql.Append("  A.StorageNumberRaw AS CunRu,0 AS ZhiQu,A.StorageNumberRaw AS ShiCun,C.numAgent,A.StorageNumberRaw*C.numAgent AS MoenyFee");
            strSqlPara.Append("  FROM dbo.Dep_StorageInfo A INNER JOIN dbo.Depositor B ON A.AccountNumber=B.AccountNumber");
            strSqlPara.Append("  INNER JOIN dbo.WB C ON B.WBID=C.ID");
            strSqlPara.Append("  WHERE DATEDIFF(DAY,A.StorageDate,GETDATE())>" + objDay);
            if (WBName != "")
            {
                strSqlPara.Append("   AND C.strName = '" + WBName + "'");
            }
            if (dtStart != "")
            {
                strSqlPara.Append("   AND A.StorageDate> '" + dtStart + "'");
            }
            if (dtEnd != "")
            {
                strSqlPara.Append("   AND A.StorageDate < '" + dtEnd + "'");
            }
            strSql.Append(strSqlPara.ToString());

            DataTable dt = SQLHelper.ExecuteDataTable(strSql.ToString());
            Repeater1.DataSource = dt;
            Repeater1.DataBind();

            numCunRu = 0; numZhiQu = 0; numShiCun = 0; numMoenyFee = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                numCunRu += Convert.ToDouble(dt.Rows[i]["CunRu"]);
                numZhiQu += Convert.ToDouble(dt.Rows[i]["ZhiQu"]);
                numShiCun += Convert.ToDouble(dt.Rows[i]["ShiCun"]);
                numMoenyFee += Convert.ToDouble(dt.Rows[i]["MoenyFee"]);
            }
            numCunRu = Math.Round(numCunRu, 2);
            numZhiQu = Math.Round(numZhiQu, 2);
            numShiCun = Math.Round(numShiCun, 2);
            numMoenyFee = Math.Round(numMoenyFee, 2);


        }

        public string GetOperateInfo(object Dep_SID)
        {
            string strReturn = "";
            //查询当前的这笔记录是否已经有结算的记录
            object objAfentFeeID = SQLHelper.ExecuteScalar("SELECT ID FROM dbo.SA_AgentFee WHERE Dep_StorageInfoID="+Dep_SID);
            if (objAfentFeeID == null || objAfentFeeID.ToString() == "")
            {
                strReturn = "  <a style='color:Red; font-weight:bold;' href='#' onclick='GetAgentFeeSingle("+Dep_SID+")'>未结算</a>";
               
            }
            else {
                strReturn = "<span style='color:Green'>已结算</span>";
            }
            return strReturn;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            object objWB = SQLHelper.ExecuteScalar(" SELECT  COUNT(ID) FROM dbo.WB WHERE strName ='"+txtWBID.Value.Trim()+"'");
            if (Convert.ToInt32(objWB) <= 0)
            {
                Fun.Alert("您查询的网点不存在");
                return;
            }
            if (Qdtstart.Value.Trim() == "")
            {
                Fun.Alert("请输入开始查询日期");
                return;
            }
            if (Qdtend.Value.Trim() == "")
            {
                Fun.Alert("请输入结束查询日期");
                return;
            }
            BindData(txtWBID.Value.Trim(), Qdtstart.Value.Trim(), Qdtend.Value.Trim());

        }


    }
}